use std::{cell::RefCell, rc::Rc, string};

use deno_core::{op2, v8, OpState};
use QcCore::scene_system::scene::Scene;
use QcUI::component::{Button, Canvas, Panel};

use crate::utils::GoExt;

#[op2]
pub fn opGetPanelSpacing<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponent::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get(index) {
                    let node = node.asAny().downcast_ref::<Panel>().unwrap();
                    return serde_v8::to_v8(scope, node.spacing).unwrap();
                }
            }
        }
    }

    v8::undefined(scope).into()
}

#[op2]
pub fn opSetPanelSpacing<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    spacing: f32,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponentMut::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get_mut(index) {
                    let node = node.asAnyMut().downcast_mut::<Panel>().unwrap();
                    node.spacing = spacing;
                }
            }
        }
    }

    v8::undefined(scope).into()
}

#[op2]
pub fn opGetPanelOrientation<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponent::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get(index) {
                    let node = node.asAny().downcast_ref::<Panel>().unwrap();

                    let orientation = node.orientation as i32;

                    return serde_v8::to_v8(scope, orientation).unwrap();
                }
            }
        }
    }

    v8::undefined(scope).into()
}

#[op2]
pub fn opSetPanelOrientation<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    mode: i32,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponentMut::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get_mut(index) {
                    let node = node.asAnyMut().downcast_mut::<Panel>().unwrap();
                    node.set_orientation(mode);
                }
            }
        }
    }

    v8::undefined(scope).into()
}
